package com.tuzhh.htmlrtspplayer.handler;

import com.tuzhh.htmlrtspplayer.bean.SessionInfo;
import com.tuzhh.htmlrtspplayer.bean.WebSocketConfig;
import com.tuzhh.htmlrtspplayer.service.LocalControlService;
import com.tuzhh.htmlrtspplayer.service.RemoteControlService;
import com.tuzhh.htmlrtspplayer.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;
import java.util.Map;

/**
 * @author Ricky
 * @date 2021/6/25
 */
public class Handler {
    public static void handleTextMessage(String protocal, WebSocketSession session, TextMessage message) throws Exception {
        SessionInfo sessionInfo = SessionInfo.get(session.getId());
        String strMsg = message.getPayload();
        System.out.println("收到" + protocal + "消息:[" + session.getId() + "]\r\n" + strMsg);
        List<String> lines = Utils.msg2lines(strMsg);
        Map<String, String> hsmpVal = Utils.list2key(lines, ":", 1);

        String seq = hsmpVal.get("seq");
        if ("data".equals(protocal)) {
            if (sessionInfo != null) {
                return;
            }
            String channel = hsmpVal.get("channel");

            if (StringUtils.isBlank(channel)) {
                session.sendMessage(new TextMessage("ERROR\r\n\r\n"));
                return;
            }
            String rtspChannel = channel.trim().substring(channel.indexOf(" ")).trim();
            sessionInfo = SessionInfo.getByChannel(rtspChannel);
            if (sessionInfo == null) {
                session.sendMessage(new TextMessage("UNKNOWN\r\n\r\n"));
                return;
            }

            SessionInfo.add(session.getId(), sessionInfo);
            sessionInfo.getLocalControlService().attachRtpChannel(session);

            String s = WebSocketConfig.PROXY_PROTOCOL + "/" + WebSocketConfig.PROXY_VERSION + " 200 OK" + "\r\n" + "seq: " + seq + "\r\n" + "\r\n";
            session.sendMessage(new TextMessage(s));
            System.out.println("发送消息给" + protocal + "\r\n" + s);
        } else if ("control".equals(protocal)) {

            if (sessionInfo != null) {
                sessionInfo.getLocalControlService().recvMsg(strMsg);
                return;
            }

            String host = hsmpVal.get("host");
            String sport = hsmpVal.get("port");
            String url = hsmpVal.get("url");
            if (StringUtils.isEmpty(host) || StringUtils.isEmpty(sport)) {
                session.sendMessage(new TextMessage("UNKNOWN\r\n\r\n"));
                return;
            }
            int port = Integer.parseInt(sport);

            sessionInfo = new SessionInfo();
            sessionInfo.setHost(host);
            sessionInfo.setPort(port);
            sessionInfo.setUrl(url);
            SessionInfo.add(session.getId(), sessionInfo);
            sessionInfo.setLocalControlService(new LocalControlService((sessionInfo)));
            sessionInfo.setRemoteControlService(new RemoteControlService(sessionInfo));
            sessionInfo.getLocalControlService().attachDataChannel(session);
            sessionInfo.getRemoteControlService().start();
            sessionInfo.getLocalControlService().start();

            String channel = sessionInfo.getChannel();
            String sessionId = sessionInfo.getSessionId();
            String channelStr = "localhost-" + sessionId + " " + channel;
            String s = WebSocketConfig.PROXY_PROTOCOL + "/" + WebSocketConfig.PROXY_VERSION + " 200 OK" + "\r\n" + "seq: " + seq + "\r\n" + "channel: " + channelStr + "\r\n" + "\r\n";
            session.sendMessage(new TextMessage(s));
            System.out.println("发送消息给" + protocal + "\r\n" + s);
        }
    }
}
